package com.xiaolei48.controller;

import com.xiaolei48.entity.Foods;
import com.xiaolei48.service.FoodsService;
import com.xiaolei48.utils.ResponseResult;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 菜品信息 前后端交互类
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/api/foods")
public class FoodsController {

    @Autowired
    private FoodsService foodsService;

    /**
     * 添加菜品信息
     */
    @PostMapping("/add.do")
    @CircuitBreaker(name = "menuService", fallbackMethod = "fallbackAddFoodsInfo")
    public ResponseResult addFoodsInfo(@RequestBody Foods foods, HttpServletRequest request) {
        try {
            if (foodsService.doAddFoodsInfo(foods)) {
                return ResponseResult.build(true, "添加菜品信息成功！");
            } else {
                return ResponseResult.build(false, "添加菜品信息失败，可能是当前菜品信息已存在！");
            }
        } catch (Exception e) {
            return ResponseResult.error("添加菜品信息异常：" + e.getMessage());
        }
    }

    /**
     * 添加菜品信息失败时的降级方法
     */
    public ResponseResult fallbackAddFoodsInfo(@RequestBody Foods foods, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 添加菜品信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 修改菜品信息
     */
    @PostMapping("/modify.do")
    @CircuitBreaker(name = "menuService", fallbackMethod = "fallbackModifyFoodsInfo")
    public ResponseResult modifyFoodsInfo(@RequestBody Foods foods, HttpServletRequest request) {
        try {
            if (foodsService.doModifyFoodsInfo(foods)) {
                return ResponseResult.build(true, "修改菜品信息成功！");
            } else {
                return ResponseResult.build(false, "修改菜品信息失败，可能是当前菜品信息不存在！");
            }
        } catch (Exception e) {
            return ResponseResult.error("修改菜品信息异常：" + e.getMessage());
        }
    }

    /**
     * 修改菜品信息失败时的降级方法
     */
    public ResponseResult fallbackModifyFoodsInfo(@RequestBody Foods foods, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 修改菜品信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 删除菜品信息
     */
    @PostMapping("/delete.do")
    @CircuitBreaker(name = "menuService", fallbackMethod = "fallbackDeleteFoodsInfo")
    public ResponseResult deleteFoodsInfo(@RequestBody Foods foods, HttpServletRequest request) {
        try {
            if (foodsService.doDeleteFoodsInfo(foods)) {
                return ResponseResult.build(true, "删除菜品信息成功！");
            } else {
                return ResponseResult.build(false, "删除菜品信息失败，可能是当前菜品信息不存在！");
            }
        } catch (Exception e) {
            return ResponseResult.error("删除菜品信息异常：" + e.getMessage());
        }
    }

    /**
     * 删除菜品信息失败时的降级方法
     */
    public ResponseResult fallbackDeleteFoodsInfo(@RequestBody Foods foods, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 删除菜品信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 获取菜品信息
     */
    @PostMapping("/get.do")
    @CircuitBreaker(name = "menuService", fallbackMethod = "fallbackGetFoodsInfo")
    public ResponseResult getFoodsInfo(@RequestBody Foods foods, HttpServletRequest request, HttpServletResponse response) {
        try {
            Foods pFoods = foodsService.doGetFoodsInfo(foods);
            return ResponseResult.build(pFoods != null, null, pFoods);
        } catch (Exception e) {
            return ResponseResult.error("获取菜品信息异常：" + e.getMessage());
        }
    }

    /**
     * 获取菜品信息失败时的降级方法
     */
    public ResponseResult fallbackGetFoodsInfo(@RequestBody Foods foods, HttpServletRequest request,
                                               HttpServletResponse response, Throwable throwable) {
        System.err.println("断路器触发 - 获取菜品信息失败: " + throwable.getMessage());

        // 返回默认菜品信息或缓存数据
        Foods defaultFood = new Foods();
        defaultFood.setName("临时菜品信息"); // 修改为正确的属性名
        defaultFood.setDescript("当前菜品详情暂时无法获取，请稍后再试"); // 修改为正确的属性名

        return ResponseResult.build(false, "系统繁忙，返回临时数据", defaultFood);
    }

    /**
     * 获取菜品列表
     */
    @GetMapping("/list.do")
    @CircuitBreaker(name = "menuService", fallbackMethod = "fallbackGetFoodsInfoList")
    public ResponseResult getFoodsInfoList(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Foods> list = foodsService.doGetFoodsInfoList();
            return ResponseResult.build(list != null, null, list);
        } catch (Exception e) {
            return ResponseResult.error("获取菜品列表异常：" + e.getMessage());
        }
    }

    /**
     * 获取菜品列表失败时的降级方法
     */
    public ResponseResult fallbackGetFoodsInfoList(HttpServletRequest request, HttpServletResponse response,
                                                   Throwable throwable) {
        System.err.println("断路器触发 - 获取菜品列表失败: " + throwable.getMessage());

        // 返回预设的菜品列表或空列表
        return ResponseResult.build(false, "系统繁忙，返回部分数据", Collections.emptyList());
    }

    /**
     * 查询菜品列表
     */
    @PostMapping("/query")
    @CircuitBreaker(name = "menuService", fallbackMethod = "fallbackQueryFoodsInfoList")
    public ResponseResult queryFoodsInfoList(@RequestBody Foods foods) {
        try {
            List<Foods> foodsList = foodsService.doQueryFoodsInfoList(foods);
            return ResponseResult.success("查询成功", foodsList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("系统错误：" + e.getMessage());
        }
    }

    /**
     * 查询菜品列表失败时的降级方法
     */
    public ResponseResult fallbackQueryFoodsInfoList(@RequestBody Foods foods, Throwable throwable) {
        System.err.println("断路器触发 - 查询菜品列表失败: " + throwable.getMessage());

        // 返回预设的菜品列表或缓存数据
        return ResponseResult.success("查询部分数据", Collections.emptyList());
    }
}